import 'package:flutter_account_book/utils/db/db_helper.dart';
import 'package:json_annotation/json_annotation.dart';
part 'pay_method.g.dart';

@JsonSerializable()
class PayMethod extends DbModel {
  @JsonKey(required: true)
  int id = 0;
  @JsonKey(required: true)
  String name = "";
  // 0内置图标 1文本缩写图标 2应用上传后图标
  @JsonKey(required: true, defaultValue: 0)
  int type = 0;
  String? iconPath;
  int order = 0;

  PayMethod() : super.fromJson({});

  factory PayMethod.fromJson(data) => _$PayMethodFromJson(data);

  @override
  Map<String, dynamic> toJson() => _$PayMethodToJson(this);

  save() async {
    bool has = await DbHelper.helper.payMethodDao.has(id);
    if (!has) {
      await DbHelper.helper.payMethodDao.insert(this);
    } else {
      await DbHelper.helper.payMethodDao.update(this);
    }
  }
}
